package Weapon

import (
	codecs "sanguo/codec/cs"
	"sanguo/node/common/com_type"
	"sanguo/node/common/transaction"
	"sanguo/node/node_game"
	"sanguo/node/node_game/module/role"
	"sanguo/node/node_game/user"
	table_GamePlay "sanguo/node/table/excel/table/GamePlay"
	cs_message "sanguo/protocol/cs/message"
	"time"

	"github.com/golang/protobuf/proto"
)

type transactionWeaponDemountSoul struct {
	transaction.TransactionBase
	user    *user.User
	req     *codecs.Message
	resp    *cs_message.WeaponDemountSoulToC
	errcode cs_message.EnumType
}

func (this *transactionWeaponDemountSoul) GetModuleName() string {
	return "Weapon"
}

func (this *transactionWeaponDemountSoul) GetReplyMsg() proto.Message {
	return &cs_message.WeaponDemountSoulToC{}
}

func (this *transactionWeaponDemountSoul) SetExpiredTime(expired time.Time) {
	this.Expired = expired
}

func (this *transactionWeaponDemountSoul) GetExpiredTime() time.Time {
	return this.Expired
}

func (this *transactionWeaponDemountSoul) Begin() {

	this.resp = &cs_message.WeaponDemountSoulToC{}
	msg := this.req.GetData().(*cs_message.WeaponDemountSoulToS)

	roleModule := this.user.GetModuleByName("role").(*role.RoleModule)
	role := roleModule.CurrentRole()
	if role == nil || !role.IsRoleModleData() {
		node_game.Infoln(role.GetUserID(), "WeaponDemountSoul fail : role is nil")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}
	node_game.Infof("%s %d Call WeaponDemountSoul wid:%d sid:%d ", role.GetUserID(), role.GetID(), msg.GetWeaponId(), msg.GetSoulId())

	//验证功能开启
	if role.GetLev() < table_GamePlay.Table_.IDMap[com_type.GamePaly_Soul].UnlockLevel {
		node_game.Infoln(role.GetUserID(), role.GetID(), "Soul System is not Unlock")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	//武器卸下武魂
	weapon := role.GetRoleWeapon().GetWeapon(msg.GetWeaponId())
	if weapon == nil {
		node_game.Infoln(role.GetUserID(), role.GetID(), "WeaponDemountSoul fail:Weapon is nil")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	for _, soulId := range msg.GetSoulId() {
		soul := role.GetRoleSoul().GetSoul(soulId)
		if soul == nil {
			node_game.Infoln(role.GetUserID(), role.GetID(), "WeaponDemountSoul fail:Soul is nil")
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}

		if !role.GetRoleSoul().DemountSoul(soul) {
			node_game.Infoln(role.GetUserID(), role.GetID(), "WeaponDemountSoul fail:DemountSoul fail")
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}

		if !role.GetRoleWeapon().DemountSoulFromWeapon(weapon, soul) {
			node_game.Infoln(role.GetUserID(), role.GetID(), "WeaponDemountSoul fail:DemountSoulFromWeapon fail")
			this.errcode = cs_message.EnumType_ERROR
			this.user.EndTrans(this)
			return
		}
	}

	if role.GetWeapon1() == msg.GetWeaponId() || role.GetWeapon2() == msg.GetWeaponId() {
		//重新计算战斗属性
		role.TotalBattleAttr()
	}

	//发送背包变化
	role.OnWeaponDirty()
	role.OnSoulDirty()

	node_game.Infoln(role.GetUserID(), role.GetID(), "WeaponDemountSoul OK")
	this.errcode = cs_message.EnumType_OK
	this.user.EndTrans(this)
}

func (this *transactionWeaponDemountSoul) End() {
	this.resp.Errcode = cs_message.EnumType(this.errcode).Enum()
	this.user.Reply(this.req.GetSeriNo(), this.resp)
}

func (this *transactionWeaponDemountSoul) Timeout() {
	this.resp.Errcode = cs_message.EnumType(cs_message.EnumType_RETRY).Enum()
	this.user.EndTrans(this)
}

func init() {
	user.RegisterTransFunc(&cs_message.WeaponDemountSoulToS{}, func(user *user.User, msg *codecs.Message) transaction.Transaction {
		return &transactionWeaponDemountSoul{
			user: user,
			req:  msg,
		}
	})
}
